package com.mew.chess.core.game.pokdeng;

import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.pokdeng.game.PokDengGameManager;
import com.mew.chess.core.game.pokdeng.game.PokDengSeatManager;
import com.mew.chess.core.game.pokdeng.game.PokDengTableManager;
import com.mew.chess.core.game.pokdeng.game.common.PokDengConstantValue;
import com.mew.chess.core.game.pokdeng.game.common.PokDengLinkType;
import com.mew.chess.core.game.pokdeng.game.common.PokDengPushCMDMapType;
import com.mew.chess.core.game.pokdeng.game.message.PokDengBetRequest;
import com.mew.chess.core.game.pokdeng.game.message.PushBetMessage;
import com.mew.chess.core.game.pokdeng.game.message.UserMessage;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@ChessPlayAnnotation(ChessHandlerMapCmdType.PokDengBet)
@Service
@Slf4j
public class PokDengBetHandler implements ChessPlayHandler<PokDengBetRequest> {

    @Autowired
    private AucRedisService aucRedisService;

    @Override
    public void handle(ZebraCommand<PokDengBetRequest> command, GameSession session) {
        try {
            PokDengBetRequest resMessage = command.getRequest();
            if (resMessage.getBetNumber() < 1||resMessage.getSeatNo()<0||resMessage.getTableId()<0) {
                command.sendFailureCode(ResponseCode.ParamInvalid.code());
                return;
            }

            long userId = session.getUserId();
            int roomId = session.getRoomId();
            int tableId = resMessage.getTableId();
            int seatNo = resMessage.getSeatNo();
            PokDengTableManager tableManager = PokDengGameManager.getInstance().getController(roomId).getTableManager(tableId);
            PokDengSeatManager seat = tableManager.getSeatMap().get(seatNo);
            if (null == tableManager ||null == seat|| null == seat.getUser()) {
                log.error("PokDengRoomManager is null userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.LoginUn.code());
                return;
            }
            if (tableManager.getLinkType() != PokDengLinkType.StartBet.code()) {
                log.error("PokDengRoomManager stop bet userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.StopBet.code());
                return;
            }
            UserMessage user=seat.getUser();
            if(user.getGoldNumber()<resMessage.getBetNumber()*5){
                log.error("PokDengRoomManager user gold on to payment:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.GoldNo.code());
                return;
            }
            if(user.getUserId()!=userId){
                log.error("PokDengRoomManager user error userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.BetError.code());
                return;
            }
            if(user.getBet()>0){
                log.error("PokDengRoomManager repetitive bet userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.BetError.code());
                return;
            }
            user.bet(resMessage.getBetNumber());

            PushBetMessage message = tableManager.getPushBetMessage();
            message.setGid(user.getGid());
            message.setSeatNo(seatNo);
            message.setBet(resMessage.getBetNumber());
            List<GameSession> sessionList = tableManager.getOnlineUser().values().stream().filter(u -> u.getUserId() != userId).collect(Collectors.toList());
            ChessPushMessageUtil.broadcastMessage(sessionList, PokDengPushCMDMapType.BetInfoMessage.code(),message);
            tableManager.skipBetLink();
            //参与
            aucRedisService.chessBetLock(userId, PokDengConstantValue.OVER_TIME); //参与
        }catch (Exception e){
            e.printStackTrace();
            command.sendFailureCode(ResponseCode.SysException.code());
        }
        command.sendSuccessMsg();
    }

}
